--GAME ENGINE FEITO POR--
--TIAGO ARAUJO AKA AZNAG88--
--MADE IN--
--PORTUGAL|PORTO--


----FUNCOES----

function blitmap() 
	frame = frame + 1
	if frame > frameSkip then
		frame = 0
		camara(char) 
		tOnX= math.floor(scrW/tsize) 
		tOnScreenW = math.floor(tOnX + 480 / tsize + 1) 
		tOnY = math.floor(scrH/tsize)
		tOnScreenH = math.floor(tOnY + 272 / tsize + 1)
		if tOnScreenW >= #map[1] then 
			tOnScreenW = #map[1]
		end	
		if tOnScreenH >= #map then
			tOnScreenH = #map
		end
		for H = tOnY + 1, tOnScreenH do 
			for W = tOnX + 1, tOnScreenW do 
				local tile = map[H][W] 
				local mapX = ((W * tsize - tsize) - scrW)
				local mapY = ((H * tsize) - tsize)
				screen:blit(mapX, mapY, tiles, 0, tile * tsize, 0, tsize, tsize)
			end
		end
	end
end

function camara(object) 
	local scrW_max = (#map[1] * tsize) - 480
	if pad:right() and object.x > 200 and scrW < scrW_max then
		scrW = scrW + 1
	end
	if pad:right() then
		if scrW == 0 or scrW == scrW_max then
			object.speedX = 1
		elseif scrW > 0 and scrW < scrW_max then
			object.speedX = 0.1
		end
		if scrW > 0 and object.x < 200 then
			object.speedX = 1
		end
	elseif pad:left() then
		if scrW < scrW_max and object.x > 200 then
			object.speedX = 1
		end
	end
end

function getobjectCorners(cornerX, cornerY, object) 
	down = math.floor(((cornerY + object.height - 1) / tsize)+1)
	up = math.floor((cornerY / tsize)+1)
	left = math.floor((cornerX / tsize)+1)
	right = math.floor(((cornerX + object.width - 1) / tsize)+1)
	middleY = math.floor(((cornerY + (object.height/2) - 1)/ tsize)+1)
	middleY2 = math.floor(((cornerY + (object.height/4) - 1)/ tsize)+1)
	middleY3 = math.floor(((cornerY + (object.height/1.3) - 1)/ tsize)+1)
	middleX = math.floor(((cornerX + (object.width/2) - 1)/ tsize)+1)
	middleX2 = math.floor(((cornerX + (object.width/4) - 1)/ tsize)+1)
	middleX3 = math.floor(((cornerX + (object.width/1.3) - 1)/ tsize)+1)
end

function tilecollision(dirX, dirY, object) 
	object= char or object
	object.x = object.x + object.speedX * dirX	
	object.y = object.y + object.speedY * dirY 
	
	getobjectCorners((object.x + scrW ) , object.y, object) 
	
	if (dirX == 1) then 
		if (collisionmap[down][right] == 0 and collisionmap[up][right] == 0) and 
		(collisionmap[middleY][right] == 0 and collisionmap[middleY2][right] == 0 and collisionmap[middleY3][right] == 0) then
			fall(char) 
		else
			object.x = (((right - 1) * tsize) - object.width) - scrW 
		end	
	end
	if (dirX == -1) then 
		if (collisionmap[down][left] == 0 and collisionmap[up][left] == 0) and 
		(collisionmap[middleY][left] == 0 and collisionmap[middleY2][left] == 0 and collisionmap[middleY3][left] == 0) then	
			fall(char) 
		else
			object.x = ((left * tsize) - scrW) 
		end
	end
	if (dirY == 1) then 
		if (collisionmap[down][left] == 0 and collisionmap[down][right] == 0) and 
		(collisionmap[down][middleX] == 0 and collisionmap[down][middleX2] == 0 and collisionmap[down][middleX3] == 0) then
		else
			object.y = ((down-1) * tsize) - object.height 
			gravity = 10 
			object.jump = false 
		end
	end
	if (dirY == -1) then 
		if (collisionmap[up][left] == 0 and collisionmap[up][right] == 0) and 
		(collisionmap[up][middleX] == 0 and collisionmap[up][middleX2] == 0 and collisionmap[up][middleX3] == 0) then	
		else
			object.y = (up * tsize) 
			gravity = 0 
			object.jump = true 
		end
	end
end

function movement(object) 
	if pad:right() and object.x <= 480 - tsize and not object.jump == true then
		object.side = "right"
		object.img = animation(object,run_right,7)
		tilecollision(1, 0, object)
	elseif pad:left() and object.x >= 0 and not object.jump == true then
		object.side = "left"
		object.img = animation(object,run_left,7)
		tilecollision(-1, 0, object)
	elseif not pad:left() and not pad:right() then
		if object.side == "right" and not object.jump == true then
			object.img = animation(object,stand_right,7)
		elseif object.side == "left" and not object.jump == true then
			object.img = animation(object,stand_left,7)
		end
	end
	if pad:cross() and oldpad:cross() ~= pad:cross() then
		object.jump = true
	end
	if object.jump == true then
		jump(char)
	end
end

function jump(object) 
	if gravity > 0 then 
		gravity = gravity - 0.75
		if object.side == "right" then
			object.img = jump_right
		elseif object.side == "left" then
			object.img = jump_left
		end
		tilecollision(0,-1,object) 
	elseif gravity <= 0 then 
		if object.side == "right" then
			object.img = fall_right
		elseif object.side == "left" then
			object.img = fall_left
		end
		tilecollision(0,1,object) 
	end
end

function fall(object) 
	if object.jump == false then 
		getobjectCorners(object.x + scrW, object.y + 1, object)
		if collisionmap[down][left] == 0 and collisionmap[down][right] == 0 and collisionmap[down][middleX] == 0 and collisionmap[down][middleX2] == 0 and collisionmap[down][middleX3] == 0 then
			gravity = 0 
			object.jump = true
		end
	end
end

function animation(objectTable, imgTable, interval) 	
	objectTable.Time = objectTable.Time + 1
	local numFrames = table.getn(imgTable) 
	local timer = objectTable.Time

	if timer >= (numFrames * interval) then 
		objectTable.Time = 0
	end 
	
	for t = 1, numFrames do 
		objectTable.width = imgTable[t]:width()
		objectTable.height = imgTable[t]:height()
		if timer >= interval * (t - 1) and timer <= interval * t then 
			img = imgTable[t] 
			break 
		end
	end
	return img 
end